package cn.biplam.back.operator.systemManage.sysprivi.user.table;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Insert;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.SysManageCode.UserPriviOptCode;
import cn.biplam.common.struc.SysStruc.UserPriviStruc;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 *  用户权限表操作
 * @author XiaoRuxing Dec 14, 20092:38:10 PM
 * @version 1.0
 */
public class UserPriviOpt extends DataOperator{

	/**用户权限表名称*/
	public static final String Table_Name=UserPriviStruc.Table_Name;

	static final String Op_ID=UserPriviOptCode.Op_ID;
	
	public UserPriviOpt() throws ErrorMessage {
		super(Table_Name);
	}
	
	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		if(UserPriviOptCode.Query_By_UserId.equals(opId)){
			return this.doing(sql,Operator_Type.Select);
		}else if(UserPriviOptCode.Add.equals(opId)){
			this.addUserPrivi(sql, filter);
			return this.doing(sql,Operator_Type.DML);
		}else{
			throw new ErrorMessage("Operator Exchange outport not found:"+ opId+" !");
		}
	}
	
	/**
	 *  添加用户权限
	 * @param sql
	 * @param filter
	 * @throws ErrorMessage 
	 */
	private void addUserPrivi(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		Insert insert = new Insert(this.tableName);
		String id = this.generatorRowID();
		insert.putStringFieldDenyNull(UserPriviStruc.USER_PRIVI_ID, id);
		insert.putStringFieldDenyNull(UserPriviStruc.USER_ID, filter.getValue(UserPriviStruc.USER_ID));
		insert.putStringFieldDenyNull(UserPriviStruc.PRIVI_ID, filter.getValue(UserPriviStruc.PRIVI_ID));
		sql.add(insert.getSimpleSql());
	}
}
